package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.support.v4.content.ContextCompat;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.lidroid.xutils.util.LogUtils;
import com.mission.schedule.R;
import com.mission.schedule.activity.WebViewActivity;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.utils.SharedPrefUtil;

/**
 * @Description: 用户首次安装，用户协议弹窗提示
 * @Author: 哈兹米
 * @CreateDate: 2021/10/26 6:06 下午
 */
public class UserProtocolTipDialog extends Dialog implements DialogInterface.OnDismissListener, View.OnClickListener {

    Context context;
    SharedPrefUtil sharedPrefUtil = null;
    CallBack callBack;

    public UserProtocolTipDialog(@NonNull final Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
        setCanceledOnTouchOutside(false);

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_user_protocol_tip, null);
        setContentView(view);
        TextView protocolContentTV = view.findViewById(R.id.protocol_content_tv);
        view.findViewById(R.id.no_agree_protocol_tv).setOnClickListener(this);
        view.findViewById(R.id.agree_protocol_tv).setOnClickListener(this);
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        //拼接字符串
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder("感谢您选择时间表APP!我们非常重视您的个人信息和隐私保护。为了更好地保障您的个人权益，在您使用我们的产品前，请务必审慎阅读");

        /**
         * 服务协议
         */
        SpannableString span = new SpannableString("《隐私政策》");
        //服务协议点击事件
        span.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url", "http://www.keytimeapp.com/Agreement.html");
                intent.putExtra("title", "隐私政策");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
//                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        }, 0, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置颜色、下划线
        span.setSpan(new ForegroundColorSpan(Color.parseColor("#FF5733")),
                0,
                span.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        spanBuilder.append(span);

        spanBuilder.append("和");
        /**
         * 用户协议
         */
        SpannableString span1 = new SpannableString("《用户协议》");
        //服务协议点击事件
        span1.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, WebViewActivity.class);
                intent.putExtra("url", "http://www.keytimeapp.com/huiyuan1.html");
                intent.putExtra("title", "用户协议");
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint ds) {
//                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        }, 0, span1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        //设置颜色、下划线
        span1.setSpan(new ForegroundColorSpan(Color.parseColor("#FF5733")),
                0,
                span1.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        );
        spanBuilder.append(span1);
        spanBuilder.append("内的所有条款，尤其是:1 .我们对您的个人信息的收集/保存/使用/对外提供/保护等规则条款，以及您的用户权利等条款;2.约定我的限制责任、免责条款;3.其他以颜色或加粗进行标识的重要条款。如您对以议有任何疑问，可通过人工客服与我们联系。您点击\"同意并继续”的行为即表示您已阅读完毕并同意以上协议的全部内容。如您同意以上协议内容，请点击\"同意并继续”，开始使用我们的产品和服务！");

        protocolContentTV.setText(spanBuilder);
        protocolContentTV.setMovementMethod(LinkMovementMethod.getInstance());
        //设置高亮颜色透明，因为点击会变色
        protocolContentTV.setHighlightColor(ContextCompat.getColor(context, R.color.transparent));
    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        LogUtils.d("11111");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            LogUtils.d("11111");
            return true;
        }
        return false;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //不同意
            case R.id.no_agree_protocol_tv:
                if (callBack != null) {
                    callBack.reject();
                }
                dismiss();
                break;
            //同意
            case R.id.agree_protocol_tv:
                if (callBack != null) {
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.ISUSERFIRSTINSTALL, "非第一次");
                    callBack.agree();
                }
                dismiss();
                break;

        }
    }

    public interface CallBack {
        void agree();

        void reject();
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
